package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

/**
 * @Title: autoGetUserInfo* @Description: 定时调用，将客户信息表中的数据汇总至customer中
 * @author: tian @date 2021-12-28 14:00:00
 */

class autoSyncPADBData implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoSyncPADBData.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        String dealSQL = "";
        int insertRow = 0;
        try {
            long startTime = System.currentTimeMillis();


              //悲观锁
            if (KHDBConfig.getParam(eliteBean, "DXLOCK") == "OPEN") {
                JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
                //将锁开启
                String updatesql = '''update sysc_parameter set PARAMETERVALUE='CLOSE' where PARAMETER_ID='DXLOCK' ''';
                jt.update(updatesql);
                //查询DWS.promote_open 表
                List Promote_OpenList = eliteBean.chaxun_sql("select  cust_mobiletelephone,creat_date,f_channel,f_xf_channel,investorid,f_status,opendate,f_register_time,f_operation_step,f_last_operation_time,innerentry from dws.promote_open where  NOW()-INTERVAL 120 HOUR < creat_date  order by creat_date asc  ", "dataSource");
                if (Promote_OpenList.size() > 0) {
                    for (int i = 0; i < Promote_OpenList.size(); i++) {
                        Map Promote_OpenMap = Promote_OpenList.get(i);
                        String mobile = Promote_OpenMap.get("cust_mobiletelephone") == null ? '' : Promote_OpenMap.get("cust_mobiletelephone");
//手机号
                        String creat_date = Promote_OpenMap.get("creat_date") == null ? '' : Promote_OpenMap.get("creat_date");
//预约时间
                        String f_channel = Promote_OpenMap.get("f_channel") == null ? '' : Promote_OpenMap.get("f_channel");//主渠道
                        String f_xf_channel = Promote_OpenMap.get("f_xf_channel") == null ? '' : Promote_OpenMap.get("f_xf_channel");//细分渠道
                        String zz3 = Promote_OpenMap.get("investorid") == null ? '' : Promote_OpenMap.get("investorid");
//资金账号
                        String f_cloud_state = Promote_OpenMap.get("f_status") == null ? '' : Promote_OpenMap.get("f_status");
//云平台状态
                        String zz4 = Promote_OpenMap.get("opendate") == null ? '' : Promote_OpenMap.get("opendate");
//开户时间
                        String f_register_time = Promote_OpenMap.get("f_register_time") == null ? '' : Promote_OpenMap.get("f_register_time");
//注册时间
                        String f_operation_step = Promote_OpenMap.get("f_operation_step") == null ? '' : Promote_OpenMap.get("f_operation_step");
//操作步骤
                        String f_last_operation_time = Promote_OpenMap.get("f_last_operation_time") == null ? '' : Promote_OpenMap.get("f_last_operation_time");
//操作时间
                        String innerentry = Promote_OpenMap.get("innerentry") == null ? '' : Promote_OpenMap.get("innerentry");
//预约来源
                        //查询 account_information表
                        List AccountList = eliteBean.chaxun_sql("select mobile,customerid,channel,channels,f_cloud_state,opendate,f_register_time,appointment_time from account_information where mobile ='" + mobile + "' and appointment_time='" + creat_date + "' and channel='" + f_channel + "'and channels='" + f_xf_channel + "' and task_type  ='CK_01' and NOW()-INTERVAL 120 HOUR < createtime order by createtime asc  ", "dataSource");
                        if (AccountList.size() > 0) {
                            String str1=null;
                            String str2=null;
                            Map AccountMap = AccountList.get(0);
                            String mobile2 = AccountMap.get("mobile") == null ? '' : AccountMap.get("mobile");
                            String channel = AccountMap.get("channel") == null ? '' : AccountMap.get("channel");
                            String channels = AccountMap.get("channels") == null ? '' : AccountMap.get("channels");
                            String appointment_time2 = AccountMap.get("appointment_time") == null ? '' : AccountMap.get("appointment_time");
                            str1 = mobile + creat_date + f_channel + f_xf_channel;
                            str2 = mobile2 + appointment_time2 + channel + channels;
                            //如果值不一致或者任务表里不存在的新增
                            if (!str1.equals(str2)) {

                                //新增到临时任务表
                                dealSQL = "insert into account_information(rec_id,task_guid,task_type,createtime,deal_flag,mobile,appointment_time,channel,channels,customerid,f_cloud_state,opendate,f_register_time,f_operation_step,f_last_operation_time,innerentry)values(uuid(),uuid(),'CK_01',NOW(),'0','" + mobile + "','" + creat_date + "','" + f_channel + "','" + f_xf_channel + "','" + zz3 + "','" + f_cloud_state + "','" + zz4 + "','" + f_register_time + "','" + f_operation_step + "','" + f_last_operation_time + "','" + innerentry + "')"
                                insertRow = eliteBean.update(dealSQL, "dataSource");

                                log.warn("【电销促开同步下发】新增account_information【手机号】："+mobile+"【预约时间】："+creat_date);

                                log.debug("[新增account_information]已成功新增临时任务表:" + insertRow);
                            } else {
                                log.debug("已新增临时任务表忽略处理");
                            }
                        } else {

                            log.warn("【电销促开同步下发】新增account_information【手机号】："+mobile+"【预约时间】："+creat_date);
                            //如果不存在则新增到临时任务表
                            dealSQL = "insert into account_information(rec_id,task_guid,task_type,createtime,deal_flag,mobile,appointment_time,channel,channels,customerid,f_cloud_state,opendate,f_register_time,f_operation_step,f_last_operation_time,innerentry)values(uuid(),uuid(),'CK_01',NOW(),'0','" + mobile + "','" + creat_date + "','" + f_channel + "','" + f_xf_channel + "','" + zz3 + "','" + f_cloud_state + "','" + zz4 + "','" + f_register_time + "','" + f_operation_step + "','" + f_last_operation_time + "','" + innerentry + "')"
                            insertRow = eliteBean.update(dealSQL, "dataSource");
                            log.debug("[新增account_information]已成功新增临时任务表:" + insertRow);

                        }
                    }
                }
                Map<String, String> headMap = new HashMap<String, String>();
                headMap.put("Content-Type", "application/json");
                //获取配置参数 直接调用
                String sysurl = KHDBConfig.getAPIURL(eliteBean, "EliteInner-API");
                Util.doHttpPostUseHead(sysurl + "/apiKFCustIssued", "{}", "UTF-8", 999999999, 0, headMap);
                long endtime = System.currentTimeMillis();



                log.debug("【电销促开同步下发】执行耗时:" + (endtime - startTime));

                //将锁关闭
                String updatesql2 = '''update sysc_parameter set PARAMETERVALUE='OPEN' where PARAMETER_ID='DXLOCK' ''';
                jt.update(updatesql2);
            } else {

                log.debug("【电销促开同步下发】当前数据在加锁");

            }
        } catch (Exception e) {
            log.error("电销促开同步平安数据到过河兵异常!", e);
        }
    }
}